package com.jiaxu.wifimusic;

import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jiaxu on 2016-3-25.
 */
public  class MediaScaner {
    public static final String TAG = "tagg";
    private Context context;

    public MediaScaner(Context context) {
        this.context = context;
    }

    public List<MusicInfo> getPlaylists() {
        Cursor cursor = context.getContentResolver().query(
                MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,
                MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
        List<MusicInfo> Playlists = new ArrayList<MusicInfo>();
        for (int i = 0; i < cursor.getCount(); i++) {

            MusicInfo Playlist = new MusicInfo(); //实例化歌曲信息类
            cursor.moveToNext();
            long id = cursor.getLong(cursor
                    .getColumnIndex(MediaStore.Audio.Media._ID));   //音乐id
            String title = cursor.getString((cursor
                    .getColumnIndex(MediaStore.Audio.Media.TITLE)));//音乐标题
            String artist = cursor.getString(cursor
                    .getColumnIndex(MediaStore.Audio.Media.ARTIST));//艺术家
            long duration = cursor.getLong(cursor
                    .getColumnIndex(MediaStore.Audio.Media.DURATION));//时长
            long size = cursor.getLong(cursor
                    .getColumnIndex(MediaStore.Audio.Media.SIZE));  //文件大小
            String url = cursor.getString(cursor
                    .getColumnIndex(MediaStore.Audio.Media.DATA));   //文件路径
            int isMusic = cursor.getInt(cursor
                    .getColumnIndex(MediaStore.Audio.Media.IS_MUSIC));//是否为音乐
            if (isMusic != 0) {     //只把音乐添加到集合当中
                Playlist.setId(id);
                Playlist.setTitle(title);
                Playlist.setArtist(artist);
                Playlist.setDuration(duration);
                Playlist.setSize(size);
                Playlist.setUrl(url);
                Playlists.add(Playlist);
            }
        }
        return Playlists;
    }
}
